package com.wenhai.disruptor.pushlisher;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;
import com.wenhai.disruptor.event.OrderEvent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 *
 *
 * @author wenhai
 * @date   2021/1/13
 */
public class OrderPublisher implements Runnable{
    private Disruptor<OrderEvent> disruptor;
    private CountDownLatch countDownLatch;
    public OrderPublisher(CountDownLatch countDownLatch, Disruptor<OrderEvent> disruptor) {
        this.countDownLatch = countDownLatch;
        this.disruptor = disruptor;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            OrderEventTranslator eventTranslator = new OrderEventTranslator();
            disruptor.publishEvent(eventTranslator);
        }
        countDownLatch.countDown();

    }

    private static class OrderEventTranslator implements EventTranslator<OrderEvent> {
        @Override
        public void translateTo(OrderEvent event, long sequence) {
            event.setId(new Random().nextLong());
        }
    }
}
